using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace GameUI {
	public class AddIconController:UIElementControllerBase, ITransferrable {

		InventoryWeaponController weaponController;

		private void Start() {
			weaponController=GetComponentInParent<InventoryWeaponController>();
		}

		protected override void OnPointerRelease() {
			if(InventoryUIController.instance.mouseItem==null) return;

			UpgradeInstance upgrade = TransferrableUtils.GetUpgradeFromTransferrableContent(InventoryUIController.instance.mouseItem.contentItem);
			if(upgrade==null) return;
			bool activated = upgrade.activated;
			upgrade.activated=false;
			bool canAdd = weaponController.boundWeapon.weaponInstance.CanAddUpgrade(upgrade);
			upgrade.activated=activated;
			if(!canAdd) return;
			if(!InventoryUIController.instance.TryTakeMouseItem()) return;
			if(!weaponController.boundWeapon.TryAddUpgrade(upgrade,null)) throw new System.Exception();

		}

		#region ITransferrable
		public Sprite itemSprite => null;
		public object contentItem => null;
		public bool canTake => false;
		public bool Choose() { return false; }
		public bool TryTake() { return false; }
		public void Return() { }
		public bool DoAccept(object content) {
			UpgradeInstance upgrade = TransferrableUtils.GetUpgradeFromTransferrableContent(content);
			if(upgrade==null) return false;
			if(!weaponController.boundWeapon.weaponInstance.CanAddUpgrade(upgrade)) return false;
			return true;
		}
		public bool TryAccept(object content) {
			if(!DoAccept(content)) return false;
			UpgradeInstance upgrade = TransferrableUtils.GetUpgradeFromTransferrableContent(content);
			if(!weaponController.boundWeapon.TryAddUpgrade(upgrade,null)) return false;
			return true;
		}
		#endregion
	}
}